package com.xuef.util;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.qiniu.util.Json;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Set;

/**
 * Created by moveb on 2018/5/21.
 */
@Service
public class JedisAdapter implements InitializingBean {
    private static final Logger logger = LoggerFactory.getLogger(JedisAdapter.class);

    private JedisPool jedisPool = null;
    @Override
    public void afterPropertiesSet() throws Exception {
        jedisPool = new JedisPool("127.0.0.1", 6379);
    }

    public Jedis getJedis(){
        Jedis jedis = jedisPool.getResource();
        // 将getJedis抽象出来，这样添加授权就方便了
        jedis.auth("121314");
        return jedis;
    }
    /**
     * 包装jedis 的 sadd
     */
    public long sadd(String key, String val){
        Jedis jedis = null;
        try{
            jedis = getJedis();
            return jedis.sadd(key, val);
        }catch(Exception e){
            logger.info("jedis sadd 发生异常", e.getMessage());
            return 0;
        }finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    public long srem(String key, String val){
        Jedis jedis = null;
        try{
            jedis = getJedis();
            return jedis.srem(key, val);
        }catch(Exception e){
            logger.info("jedis srem 发生异常", e.getMessage());
            return 0;
        }finally {
            if(jedis != null){
                jedis.close();
            }
        }
    }

    public boolean sismember(String key, String val){
        Jedis jedis = null;
        try{
            jedis = getJedis();
            return jedis.sismember(key, val);
        }catch(Exception e){
            logger.info("jedis sismember 发生异常", e.getMessage());
            return false;
        }finally {
            if(jedis != null){
                jedis.close();
            }
        }
    }

    public List<String> lrange(String key){
        Jedis jedis = null;
        List<String> stringList = null;
        try{
            jedis = getJedis();
            stringList = jedis.lrange(key, 0, jedis.llen(key) + 1);
        }catch(Exception e){
            logger.info("jedis lrange 发生异常", e.getMessage());
            return null;
        }finally {
            if(jedis != null){
                jedis.close();
            }
        }
        return stringList;
    }

    public Set<String> smembers(String key){
        Jedis jedis = null;
        Set<String> eles = null;
        try{
            jedis = getJedis();
            eles = jedis.smembers(key);
        }catch(Exception e){
            logger.info("jedis smembers 发生异常", e.getMessage());
            return null;
        }finally {
            if(jedis != null){
                jedis.close();
            }
        }
        return eles;
    }

    public long scard(String key){
        Jedis jedis = null;
        try{
            jedis = getJedis();
            return jedis.scard(key);
        }catch(Exception e){
            logger.info("jedis scard 发生异常", e.getMessage());
            return 0;
        }finally {
            if(jedis != null){
                jedis.close();
            }
        }
    }

    public void lpush(String key, String val){
        Jedis jedis = null;
        try{
            jedis = getJedis();
            jedis.lpush(key, val);
        }catch(Exception e){
            logger.info("jedis lpush 发生异常", e.getMessage());
        }finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    public void set(String key, String val){
        Jedis jedis = null;
        try{
            jedis = getJedis();
            jedis.set(key, val);
        }catch(Exception e){
            logger.info("jedis set 发生异常", e.getMessage());
        }finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    public String get(String key){
        Jedis jedis = null;
        try{
            jedis = getJedis();
            return jedis.get(key);
        }catch(Exception e){
            logger.info("jedis get 发生异常", e.getMessage());
        }finally {
            if(jedis != null){
                jedis.close();
            }
        }
        return null;
    }

    public void setObj(String key, Object obj){
        set(key, JSON.toJSONString(obj));
    }
    public <T> T getObj(String key, Class<T> clazz){
        String val = get(key);
        if(val != null) {
            T obj = JSON.parseObject(val, clazz);
            return obj;
        }
        return null;
    }

    public List<String> brpop(int timeout, String key) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            return jedis.brpop(timeout, key);
        } catch (Exception e) {
            logger.error("brpop 发生异常" + e.getMessage());
            return null;
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
}
